<?php

/** Check if environment is development and display errors * */
function set_reporting() {
    if (DEVELOPMENT_ENVIRONMENT == true) {
        error_reporting(E_ALL);
        ini_set('display_errors', 'On');
    } else {
        error_reporting(E_ALL);
        ini_set('display_errors', 'Off');
        ini_set('log_errors', 'On');
        ini_set('error_log', ROOT . DS . 'tmp' . DS . 'logs' . DS . 'error.log');
    }
}

/** Check for Magic Quotes and remove them * */
function strip_slashes_deep($value) {
    $value = is_array($value) ? array_map('strip_slashes_deep', $value) : stripslashes($value);
    return $value;
}

function remove_magic_quotes() {
    if (get_magic_quotes_gpc()) {
        $_GET = strip_slashes_deep($_GET);
        $_POST = strip_slashes_deep($_POST);
        $_COOKIE = strip_slashes_deep($_COOKIE);
    }
}

/** Check register globals and remove them * */
function unregister_globals() {
    if (ini_get('register_globals')) {
        $array = array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
        foreach ($array as $value) {
            foreach ($GLOBALS[$value] as $key => $var) {
                if ($var == $GLOBALS[$key]) {
                    unset($GLOBALS[$key]);
                }
            }
        }
    }
}

/** Routing * */
function route_url($url) {
    global $routing;

    foreach ($routing as $pattern => $result) {
        if (preg_match($pattern, $url)) {
            return preg_replace($pattern, $result, $url);
        }
    }

    return ($url);
}

/** Main Call Function * */
function dispatch() {
    global $url;
    global $default;

    $query_string = array();

    if (!isset($url)) {
        $controller = $default['controller'];
        $action = $default['action'];
    } else {
        $url = route_url($url);
        $url_array = array();
        $url_array = explode("/", $url);

        $controller = $url_array[0];
        array_shift($url_array);

        if (isset($url_array[0])) {
            $action = $url_array[0];
            array_shift($url_array);
        } else {
            $action = 'index';
        }

        $query_string = $url_array;
    }

    $controller_name = $controller;
    $controller = ucfirst($controller);
    $dispatch = new $controller($controller_name, $action);

    if ((int) method_exists($controller, $action)) {
        call_user_func_array(array($dispatch, $action), $query_string);
    } else {
        /* Error Generation Code Here */
        echo 'Cannot find method [shared.php]';
    }
}

/** Autoload any classes that are required * */
function __autoload($class_name) {
    if (file_exists(ROOT . DS . 'library' . DS . strtolower($class_name) . '.class.php')) {
        require_once (ROOT . DS . 'library' . DS . strtolower($class_name) . '.class.php');
    } else if (file_exists(ROOT . DS . 'application' . DS . 'controllers' . DS . strtolower($class_name) . '.php')) {
        require_once (ROOT . DS . 'application' . DS . 'controllers' . DS . strtolower($class_name) . '.php');
    } else if (file_exists(ROOT . DS . 'application' . DS . 'models' . DS . strtolower($class_name) . '.php')) {
        require_once (ROOT . DS . 'application' . DS . 'models' . DS . strtolower($class_name) . '.php');
    } else {
        /* Error Generation Code Here */
        echo 'Cannot load class ' . $class_name . ' [shared.php]';
    }
}

$inflect = new Inflection();
$session = new Session();
$cache = new Cache();

set_reporting();
remove_magic_quotes();
unregister_globals();
dispatch();